﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Class_Entity;
using BussinessObject;

public partial class WebApplication_ADLiquidFilling : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            loadGrid();

        }
    }

    private void loadGrid()
    {
        LiquidFilling li = new LiquidFilling();
        BOProducts pro = new BOProducts();

        //load data to gridview
        GridView1.DataSource = pro.Load_LiquidFilling(li);
        GridView1.DataBind();
    }

    //direct to add new page
    protected void btNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("ADAddLiquidFilling.aspx");
    }


    // search model by name
    protected void btSearch_Click(object sender, EventArgs e)
    {
        LiquidFilling li = new LiquidFilling();
        BOProducts pro = new BOProducts();

        li.Model = txtSearch.Text;

        GridView1.DataSource = pro.Search_LiquidFilling(li);
        GridView1.DataBind();
    }


    // delete many object in one click
    protected void btDelete_Click(object sender, EventArgs e)
    {
        LiquidFilling li = new LiquidFilling();
        BOProducts pro = new BOProducts();

        // check every row if check box is checked or not then delete
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox cb = (CheckBox)row.FindControl("CheckBox1");
            if (cb != null && cb.Checked)
            {

                int id = Convert.ToInt32(row.Cells[1].Text);
                li.ID = id;
                pro.Delete_LiquidFilling(li);
                HttpContext.Current.Response.Write("<script type='text/javascript'>alert('" + "Delete Successfully ..." + "');</script>");
            }
        }

        loadGrid();
    }



    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        LiquidFilling li = new LiquidFilling();
        BOProducts pro = new BOProducts();
        //load information to object li
        int id = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text);

        li.ID = id;
        //delete object li in database
        pro.Delete_LiquidFilling(li);
        HttpContext.Current.Response.Write("<script type='text/javascript'>alert('" + "Delete Successfully ..." + "');</script>");
        loadGrid();
    }


    //direct to update page
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["ID"] = Convert.ToInt32(GridView1.SelectedRow.Cells[1].Text);
        Response.Redirect("ADEditLiquidFilling.aspx");
    }
}